﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.ApplyCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.ApplyHandler
{
    public class ApplyCommandHandler : IRequestHandler<ApplyCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Apply> _ApplyRepository;
        private readonly IBaseRepository<RecordSheet> _RecordRepository;

        public ApplyCommandHandler(IBaseRepository<Apply> applyRepository, IBaseRepository<RecordSheet> recordRepository)
        {
            _ApplyRepository = applyRepository;
            _RecordRepository = recordRepository;
        }

        public async Task<APIResult<object>> Handle(ApplyCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.Success;
            result.Msg = "审批成功";

            RecordSheet record = new RecordSheet();

            var apply = _ApplyRepository.GetAll().FirstOrDefault(p => p.ApplyId == request.ApplyId);
            if (apply != null)
            {
                if (request.Importance == 1)
                {
                    if (request.is_load == "不通过")
                    {
                        apply.ApprovalStatusId = (int)ApprovalStatus.已完成;
                        apply.ApprovalStatusName = ApprovalStatus.已完成.ToString();
                        apply.is_load = request.is_load;
                        apply.YuanYin = request.YuanYin;
                        _ApplyRepository.Update(apply);

                        #region 审批记录表
                        record.ApplyId = apply.ApplyId;
                        record.NodeName = request.Name + "已拒绝";
                        record.OperatorId = request.ApplyId;
                        record.OperatorName = request.ApplyPerson;
                        record.OperatorOpinion = request.YuanYin;
                        record.RecordStatusId = (int)RecordStatus.未通过;
                        record.RecordStatusName = RecordStatus.未通过.ToString();
                        record.CreateTime = DateTime.Now;
                        _RecordRepository.Add(record);
                        #endregion

                        result.Code = APIEnum.Success;
                        result.Msg = "审批成功";
                    }
                    else
                    {
                        apply.ApprovalStatusId = (int)ApprovalStatus.审核中;
                        apply.ApprovalStatusName = ApprovalStatus.审核中.ToString();
                        apply.is_load = request.is_load;
                        apply.YuanYin = request.YuanYin;
                        _ApplyRepository.Update(apply);

                        #region 审批记录表
                        record.ApplyId = apply.ApplyId;

                        record.NodeName = request.Name + "已审批";
                        record.OperatorId = request.ApplyId;
                        record.OperatorName = request.ApplyPerson;
                        record.OperatorOpinion = request.YuanYin;
                        record.RecordStatusId = (int)RecordStatus.已通过;
                        record.RecordStatusName = RecordStatus.已通过.ToString();
                        record.CreateTime = DateTime.Now;
                        _RecordRepository.Add(record);
                        #endregion

                        result.Code = APIEnum.Success;
                        result.Msg = "审批成功";
                    }
                }
                if (request.Importance == 2)
                {
                    if (request.is_load == "不通过")
                    {
                        apply.ApprovalStatusId = (int)ApprovalStatus.已完成;
                        apply.ApprovalStatusName = ApprovalStatus.已完成.ToString();
                        apply.is_load = request.is_load;
                        apply.YuanYin = request.YuanYin;
                        _ApplyRepository.Update(apply);

                        #region 审批记录表
                        record.ApplyId = apply.ApplyId;
                        record.NodeName = request.Name + "已拒绝";
                        record.OperatorId = request.ApplyId;
                        record.OperatorName = request.ApplyPerson;
                        record.OperatorOpinion = request.YuanYin;
                        record.RecordStatusId = (int)RecordStatus.未通过;
                        record.RecordStatusName = RecordStatus.未通过.ToString();
                        record.CreateTime = DateTime.Now;
                        _RecordRepository.Add(record);
                        #endregion

                        result.Code = APIEnum.Success;
                        result.Msg = "审批成功";
                    }
                    else
                    {
                        apply.ApprovalStatusId = (int)ApprovalStatus.审核通过;
                        apply.ApprovalStatusName = ApprovalStatus.审核通过.ToString();
                        apply.is_load = request.is_load;
                        apply.YuanYin = request.YuanYin;
                        _ApplyRepository.Update(apply);

                        #region 审批记录表
                        record.ApplyId = apply.ApplyId;

                        record.NodeName = request.Name + "已审批";
                        record.OperatorId = request.ApplyId;
                        record.OperatorName = request.ApplyPerson;
                        record.OperatorOpinion = request.YuanYin;
                        record.RecordStatusId = (int)RecordStatus.已通过;
                        record.RecordStatusName = RecordStatus.已通过.ToString();
                        record.CreateTime = DateTime.Now;
                        _RecordRepository.Add(record);
                        #endregion

                        result.Code = APIEnum.Success;
                        result.Msg = "审批成功";
                    }
                }
            }


            return await Task.FromResult(result);
        }
    }
}